package com.aos.erp.schools.service.schoolclass.impl;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.schools.mapper.schoolclass.DriversSchoolClassMapper;
import com.aos.erp.schools.model.coach.Coach;
import com.aos.erp.schools.model.schoolclass.*;
import com.aos.erp.schools.service.schoolclass.DriversSchoolClassService;
import com.aos.erp.schools.util.UserUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DriversSchoolClassServiceImpl extends ServiceImpl<DriversSchoolClassMapper,DriversSchoolClass> implements DriversSchoolClassService {

    @Resource private DriversSchoolClassMapper driversSchoolClassMapper;

    @Override
    public DriversSchoolClass getById(long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void addBean(AddDriversSchoolClassRo addDriversSchoolClassRo,String currentUserId) throws Exception {
        Date date=new Date();
        DriversSchoolClass dsc=new DriversSchoolClass();
        BeanUtils.copyProperties(addDriversSchoolClassRo,dsc);
        ///////////////////////////////////
        //TODO 暂时写死班级编号
        dsc.setClassNumber("68629315");
        dsc.setDriversId(Long.parseLong(UserUtil.getUserSchoolId(currentUserId)));
        ////////////////////////////////////
        dsc.setIsDel(0);//默认不删除
        dsc.setCreated(date);
        dsc.setUpdated(date);
       // driversSchoolClassMapper.save(dsc);
        baseMapper.insert(dsc);
    }

    @Override
    public PageBean<DriversSchoolClassInfoVo> queryByPage(QueryClassRo queryClassRo) throws Exception {
        String depCode=UserUtil.getUserDepCode(queryClassRo.getCurrentUserId());
        queryClassRo.setDepCode(depCode);
        Page<DriversSchoolClassInfoVo> page = new Page<DriversSchoolClassInfoVo>(queryClassRo.getPageIndex(), queryClassRo.getPageSize());
        List<DriversSchoolClassInfoVo> noticeInfoList = baseMapper.queryList(page, queryClassRo);
        page.setRecords(noticeInfoList);

        PageBean<DriversSchoolClassInfoVo> bean = new PageBean<DriversSchoolClassInfoVo>(page.getCurrent(), page.getSize(), page.getTotal());
        bean.setOptimizeCountSql(page.optimizeCountSql());
        bean.setAscs(page.ascs());
        bean.setDescs(page.descs());
        bean.setRecords(page.getRecords());
        bean.condition();


        return  bean;
    }

    @Override
    public void deleteClass(String ids) throws BizException {
//        DriversSchoolClass schoolClass=baseMapper.selectById(id);
//        if(schoolClass==null){
//            throw new BizException("班型数据错误");
//        }
//        schoolClass.setIsDel(-1);//逻辑删除
//        return baseMapper.updateById(schoolClass);

        if(ids.length() == 0) {
            throw new BizException("班型ID不能为空");
        }
        baseMapper.deleteClass(ids);
    }

    @Override
    public DriversSchoolClassInfoVo classInfo(long id) {
        return driversSchoolClassMapper.queryObject(id);
    }

    @Override
    public int updateClass(UpdateDriversSchoolClassRo updateDriversSchoolClassRo) {
        Date date=new Date();
        DriversSchoolClass dsc=new DriversSchoolClass();
        BeanUtils.copyProperties(updateDriversSchoolClassRo,dsc);
        dsc.setUpdated(date);
        System.out.println(dsc.toString());
        System.out.println(updateDriversSchoolClassRo.toString());
        return driversSchoolClassMapper.updateClass(dsc);
    }
}